﻿namespace Brjnk.GpxExplorer.Workspace
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Xml.Linq;

    /// <summary>
    /// Reprezentuje informace o zemi. Data jsou získána z xml souboru v Resources
    /// </summary>
    public class CountryInfo
    {
        public RegionInfo region;

        public double North { get; private set; }

        public double West { get; private set; }

        public double East { get; private set; }

        public double South { get; private set; }

        public double Area { get; private set; }

        public double Population { get; private set; }

        public string CurrencyCode { get; private set; }

        public string Name { get; private set; }

        public string Capital { get; private set; }

        public string Languages { get; private set; }

        public CountryInfo(RegionInfo region)
        {
            this.region = region;
            LoadInfo();
        }

        private XDocument xml;

        private void LoadInfo()
        {
            OpenXml();

            var countries = xml.Root.Elements("country");
            foreach (var country in countries)
            {
                var name = country.Element("isoAlpha3").FirstNode.ToString();
                if (string.Equals(name, region.ThreeLetterISORegionName, StringComparison.InvariantCultureIgnoreCase))
                {
                    West = double.Parse(country.Element("west").FirstNode.ToString(), System.Globalization.CultureInfo.InvariantCulture);
                    North = double.Parse(country.Element("north").FirstNode.ToString(), System.Globalization.CultureInfo.InvariantCulture);
                    East = double.Parse(country.Element("east").FirstNode.ToString(), System.Globalization.CultureInfo.InvariantCulture);
                    South = double.Parse(country.Element("south").FirstNode.ToString(), System.Globalization.CultureInfo.InvariantCulture);

                    Area = double.Parse(country.Element("areaInSqKm").FirstNode.ToString(), System.Globalization.CultureInfo.InvariantCulture);

                    Population = double.Parse(country.Element("population").FirstNode.ToString(), System.Globalization.CultureInfo.InvariantCulture);
                    CurrencyCode = country.Element("currencyCode").FirstNode.ToString();
                    Name = country.Element("countryName").FirstNode.ToString();
                    Languages = country.Element("languages").FirstNode.ToString();
                    Capital = country.Element("capital").FirstNode.ToString();

                    return;
                }
            }
            System.Diagnostics.Debug.WriteLine("Country not found!");
        }

        private void OpenXml()
        {
            if (xml == null)
            {
                var resStream = App.GetResourceStream(new Uri(@"pack://application:,,,/GpxExplorer;Component/Resources/Countries.xml"));
                xml = XDocument.Load(resStream.Stream);
            }
        }

        static CountryInfo()
        {
            try
            {
                var region = RegionInfo.CurrentRegion;
                Current = new CountryInfo(region);
            }
            catch (Exception ex) { }
        }

        public static IEnumerable<RegionInfo> GetAllRegions()
        {
            foreach (var culture in CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures))
            {
                if (culture.IsNeutralCulture || culture.LCID == 127) continue;
                yield return new RegionInfo(culture.LCID);
            }
        }

        public static CountryInfo Current { get; private set; }
    }
}